<?

// v 0.47

class Churro {
	
	public $json = array();			// (object) variables to be returned in a json format, in an ajax call 
	public $rss = null;				// TODO: this
	
	public $vars = array(); 				// object, all the variables that will be passed in to any rendered views
	
	private $ajax = FALSE; 					// if we have requested page via xmlhttp request
	private $css = array();					// any additional css we want to add;
	private $scripts = array(); 			// any additional scripts we need to use;
	private $stats = array();				// 
	
	private $phtml = 'index';				// the view/.phtml file to use in rendering.
	private $template = 'index'; 			// the default template file to use
	private $template_extension = '.phtml';	// 
	private $template_path = ''; 			// the full path to the template file
	private $theme = THEME;					// directory in public/template/ with layout wrapper files 
	private $useLayout = TRUE;				// usually disable for ajax calls
	private $view = ''; 					// the view/phtml to be inserted into template 
	
	protected $db = null; 					// database connection 
	
	public function __construct( &$db, $starttime = null ){
		$this->db = &$db;
		
		$this->json = object();
		$this->vars = object();
		$this->template_path = PUBLICROOT.'/template/'.$this->theme.'/';
		
		$this->stats = object();
		$this->stats->starttime = $starttime;
	}
	
	public function view( $view ){
		return $this->view = $view;
	}
	
	public function getView(){
		return $this->view;
	}
	
	// get or set path to the main template wrapper
	public function Template( $template = null ){
		if( !is_null($template) ){
			$this->template = $template;
		}
		
		$full_path = $this->template_path.$this->template.$this->template_extension;

		return $full_path;
	}
	
	public function TemplatePath( $full_path ){
		$pathinfo = pathinfo( $full_path );
		
		$this->template_path = $pathinfo['dirname'].'/';
		$this->template = $pathinfo['filename'];
		$this->template_extension = '.'.$pathinfo['extension'];
	}
	
	public function Theme( $theme = null ){
		if( !is_null($theme) ){
			$this->theme = $theme;
		}
		
		return $this->theme;
	}
	
	//the main function to process a view 
	public function Render( $html = '', $vars = array() ){
		
		$this->nesting ++;
		
		if( trim($html) && file_exists('../view/'.$html.'.phtml') ){
			//if we pass in a different view, use it. all good 
			$this->phtml = $html;
		} else if( file_exists('../view/'.($this->view).'.phtml') ){
			//if there is a file for the view we are looking at, use it
			$this->phtml = $this->view;
		} else {
			//default to to index
			$this->phtml = 'index';
		}
		
		//make churro object variables available to the template
		if( !count($vars) ){
			$vars = $this->vars;
		}
		
		foreach( $vars as $k=>$v ){
			$$k = $v;
		}
		
		ob_start();
			include '../view/'.$this->phtml.'.phtml';
			$view = ob_get_contents();
		ob_end_clean();
		
		if( ISDEV ){
			$tabs = str_repeat( "\t", ($this->nesting - 1) );
			$view = "\n$tabs<!-- view: $this->phtml -->\n$tabs$view\n$tabs<!-- end view: $this->phtml -->\n";
		}
		
		$this->nesting --;
		
		return $view;
	}
	
	// get or set whether is xml http request
	public function isAjax( $bool = null ){
		if( is_null($bool) ){
			return $this->ajax;
		} else if( is_bool($bool) ){
			$this->ajax = $bool;
		}
	}
	
	// this is ghetto.  will remove soon.
	public function Forward( $controller ){
		$this->$controller();
	}
	
	// disable the rendering , useful for ajax, rss etc
	public function Layout( $bool = null ){
		if( is_bool($bool) ){
			$this->useLayout = $bool;
			return true;
		}
		
		return $this->useLayout;
	}
	
	// add an additional script
	public function addScript( $script ){
		if ( !in_array($script, $this->scripts ) ){
			array_push( $this->scripts, $script );
		}
	}
	
	// return array of added scripts
	public function scripts(){
		return $this->scripts;
	}
	
	// add an additional css
	public function addCSS( $css ){
		if ( !in_array(($css), $this->scripts) ){
			array_push( $this->scripts, $script );
		}
	}
	
	// return array of added css
	public function css(){
		return $this->css;
	}
	
	// redirect the browser
	public function Redirect( $loc = '', $status = null ){
		$statusArray = array( 200 => 'OK',
							  301 => 'HTTP/1.1 301 Moved Permanently',
							  401 => 'HTTP/1.1 401 Unauthorized',
							  403 => 'HTTP/1.1 403 Forbidden',
							  404 => 'HTTP/1.1 404 Not Found',
							  418 => 'HTTP/1.1 418 I\'m a teapot' );
		if( $status && array_key_exists($status, $statusArray) ){
			header( 'Location: '.HTTPROOT.$loc, TRUE, $status );
		} else {
			header( 'Location: '.HTTPROOT.$loc );
		}
		
		die();
	}
	
	public function server_stats( $return = 'html' ){
		
		$stats = $this->db->getStats();
		
		$rendertime = round( microtime(TRUE) - $this->stats->starttime, 4 );
		$memory = round( memory_get_peak_usage() / 1048576 , 3 );
		
		if( $return == 'html' ){
			$html = "r: $rendertime
					 m: $memory MB
					 d: $stats->q 
					 c: $stats->c";
			
			return $html;
		} else {
			return array( 'r' => $rendertime, 
						  'm' => $memory,
						  'd' => $stats->q, 
						  'c' => $stats->c );
		}
		
	}
}

// end of file
// class_Churro.php